package com.eastjet.maintenance.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eastjet.maintenance.config.Code;
import com.eastjet.maintenance.domain.*;
import com.eastjet.maintenance.feign.CodeDataFeing;
import com.eastjet.maintenance.mapper.LedgerMapper;
import com.eastjet.maintenance.service.LedgerService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 杨博文
 * @description 针对表【ledger(设备台帐)】的数据库操作Service实现
 * @createDate 2024-11-25 09:23:38
 */
@Service
public class LedgerServiceImpl extends ServiceImpl<LedgerMapper, Ledger> implements LedgerService {

    @Autowired
    private LedgerMapper ledgerMapper;//注入mapper
    @Autowired
    private AssociatedDevicesServiceImpl associatedDevicesServiceImpl;//注入关联设备Service
    @Autowired
    private InitialInventoryServiceImpl inventoryServiceImpl;//注入初始库存Service
    @Autowired
    private SImageServiceImpl imageServiceImpl;//注入图片Service
    @Autowired
    private DocumentationServiceImpl documentationServiceImpl;//注入文档Service
    @Autowired
    private CodeDataFeing codeDataFeing;//自动生成编码服务


    @SneakyThrows
    @Override
    public Boolean InsertLedger(Map<String, Object> map) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date datee = df.parse(df.format(new Date()));
        String code = codeDataFeing.test(new HashMap<String, String>() {{
            put("TableName", "ledger");
        }});
        Ledger p = new Ledger();
        p.setCode(code);
        p.setName(map.get("name").toString());
        p.setType(map.get("type").toString());
        p.setSpecification(map.get("specification").toString());
        p.setUnit(map.get("unit").toString());
        p.setManufacturer(map.get("manufacturer").toString());
        p.setBrand(map.get("brand").toString());
        p.setVendor(map.get("vendor").toString());
        p.setInventoryLimit(map.get("inventoryLimit").toString());
        p.setInventoryMinimum(map.get("inventoryMinimum").toString());
        p.setCycle(map.get("cycle").toString() + map.get("replacementUnit").toString());
        p.setIndicativePrice(BigDecimal.valueOf(Double.valueOf(map.get("indicativePrice").toString())));
        p.setCreationTime(datee);
        p.setCreatedBy(map.get("usname").toString());
        p.setIs_delete(0);
        p.setRemark(map.get("remark").toString());
        boolean save = this.save(p);
        if (save) {
            //新增关联设备
            List<Object> tableData = (List<Object>) map.get("tableData");
            List<AssociatedDevices> list = new ArrayList<>();
            //循环映射设备关联数据
            tableData.forEach(item -> {
                item = (Map<String, Object>) item;
                AssociatedDevices s = new AssociatedDevices();
                s.setCode(code);
                s.setDevicenumber(((Map<?, ?>) item).get("code").toString());
                s.setName(((Map<?, ?>) item).get("name").toString());
                s.setDeviceType(((Map<?, ?>) item).get("deviceType").toString());
                s.setSpecification(((Map<?, ?>) item).get("specifications").toString());
                s.setBrand(((Map<?, ?>) item).get("brand").toString());
                s.setUseDepartments(((Map<?, ?>) item).get("useDepartments").toString());
                s.setStorageLocation(((Map<?, ?>) item).get("storageLocation").toString());
                s.setDeviceStatus("1");
                s.setEquipmentParts(((Map<?, ?>) item).get("deviceType").toString());
                s.setAssociatedQuantity(Integer.valueOf(((Map<?, ?>) item).get("actualPrice").toString()));
                s.setRemark(((Map<?, ?>) item).get("note").toString());
                s.setCreatedBy(map.get("usname").toString());
                s.setCreationTime(datee);
                s.setTombstone(0);
                list.add(s);
            });
            boolean b = associatedDevicesServiceImpl.saveBatch(list);
            //新增初始库存
            if (b) {
                //新增初期库存
                List<Object> initialInventoryData = (List<Object>) map.get("items");
                List<InitialInventory> initialInventoryList = new ArrayList<>();
                //循环映射初始库存数据
                initialInventoryData.forEach(item -> {
                    item = (Map<String, Object>) item;
                    InitialInventory s = new InitialInventory();
                    s.setCode(code);
                    s.setWarehouse(((Map<?, ?>) item).get("name").toString());
                    s.setOpeningCosts(BigDecimal.valueOf(Double.valueOf(((Map<?, ?>) item).get("cost").toString())));
                    s.setOpeningInventory(Integer.valueOf(((Map<?, ?>) item).get("quantity").toString()));
                    s.setOpeningTotals(BigDecimal.valueOf(s.getOpeningInventory()).multiply(s.getOpeningCosts()));
                    s.setRemark(((Map<?, ?>) item).get("notes").toString());
                    s.setStorageLocation(((Map<?, ?>) item).get("storageLocation").toString());
                    s.setCreationTime(datee);
                    s.setCreatedBy(map.get("usname").toString());
                    s.setTombstone(0);
                    initialInventoryList.add(s);
                });
                Boolean b1 = inventoryServiceImpl.InsertInventory(initialInventoryList);
                if (b1) {
                    //新增图片
                    List<Object> imageData = (List<Object>) map.get("fileList2");
                    List<SImage> imageList = new ArrayList<>();
                    //循环映射图片数据
                    imageData.forEach(item -> {
                        item = (Map<String, Object>) item;
                        SImage img = new SImage();
                        img.setCode(code);
                        img.setEid(Integer.valueOf(((Map<?, ?>) item).get("size").toString()));
                        img.setPath(((Map<?, ?>) item).get("name").toString());
                        img.setCreationTime(datee);
                        img.setCreatedBy(map.get("usname").toString());
                        imageList.add(img);
                    });
                    Boolean b2 = imageServiceImpl.InsertImage(imageList);
                    if (b2) {
                        //新增文档
                        List<Object> documentationData = (List<Object>) map.get("documents");
                        List<Documentation> documentationList = new ArrayList<>();
                        //循环映射文档数据
                        documentationData.forEach(item -> {
                            item = (Map<String, Object>) item;
                            Documentation doc = new Documentation();
                            doc.setCode(code);
                            doc.setFilename(((Map<?, ?>) item).get("name").toString());
                            doc.setUploadTime(datee);
                            doc.setUploader(map.get("usname").toString());
                            doc.setSize(((Map<?, ?>) item).get("size").toString());
                            doc.setEid(1);
                            doc.setCreationTime(datee);
                            doc.setCreatedBy(map.get("usname").toString());
                            documentationList.add(doc);
                        });
                        return documentationServiceImpl.InsertList(documentationList);
                    }
                }
            }
        }
        return false;
    }

    /*
     * 批量更新
     */
    @Override
    public Boolean UpdateLedger(List<Ledger> ledger) {
        return this.updateBatchById(ledger);
    }

    /*
     * 批量删除
     */
    @Override
    public Boolean DeleteLedger(List<String> ledgerId) {
        return this.removeByIds(ledgerId);
    }

    /*
     * 查询备件信息
     */
    @Override
    public List<Ledger> SelectLedger(Ledger ledger) {
        return ledgerMapper.SelectLedger(ledger);
    }
}




